var async = require('async');
var _ = require('lodash');
var siteInfoService = require('../services/site-info.service');
var categoriesService = require('../services/categories.service');
var listsService = require('../services/lists.service');
var logger = require('../../lib/logger.lib');
var modelsService = require('../services/models.service');

/**
 * 首页
 * @param {Object} req
 * @param {Object} res
 */
module.exports = function (req, res) {
  async.parallel({
    siteInfo: siteInfoService.get,
    navigation: function (callback) {
      categoriesService.navigation({ current: '/' }, callback);
    },
    cpzsCategory: function(callback){
      categoriesService.one({
        path: '/cpsj/cpzs',
        type: 'column'
      }, callback)
    },
    sjpzCategory: function(callback){
      categoriesService.one({
        path: '/cpsj/sjpz',
        type: 'column'
      }, callback)
    },
    newsCategory: function(callback){
      categoriesService.one({
        path: '/news/qyzx',
        type: 'column'
      }, callback)
    },
  }, function(err, results){
    logger.system().debug(results);
    async.parallel({
      news: function(callback){
        listsService.column({_id:results.newsCategory._id, pageSize: 4}, callback)
      },
      sjpz: function(callback){
        listsService.column({_id:results.sjpzCategory._id, pageSize: 2}, callback)
      },
      cpMode: function (callback) {
        var query = {name: "瓷砖"};
        modelsService.query({ query: query }, callback);
        // listsService.columnby({_id:results.cpzsCategory._id}, callback)
      }
    }, function(err, results1){
      logger.system().debug(results1);
      var cpMode = results1.cpMode[0];
      var extensions = cpMode.extensions;
      var seriesExt = extensions[0];
      var seriesList = seriesExt.mixed.select;
      logger.system().debug("Series List: " + seriesList);
      //=========查询指定系列的产品
      var query = {
        _id: results.cpzsCategory._id,
        series: req.query.series
      };
      if (!query.series && seriesList[0]) {
        query.series = seriesList[0].value;
      }
      query.pageSize = 4;
      listsService.column(query, function (err, results2) {
        var cpzs = results2.contents;
        var sjpz = results1.sjpz.contents;
        var news = results1.news.contents;

        //首页产品数量
        var productCount = 4;
        //首页实景铺装案例数量
        var caseCount = 2;

        //确保产品数量等于productCount，不够使用空对象填充，方便页面布局
        if (cpzs.length < productCount) {
          for (var i = cpzs.length; i < productCount; i++) {
            cpzs.push({});
          }
        }
        else if (cpzs.length > productCount) {
          while (cpzs.length != productCount) {
            cpzs.pop();
          }
        }

        //确保产品数量等于caseCount，不够使用空对象填充，方便页面布局
        if (sjpz.length < caseCount) {
          for (var i = sjpz.length; i < caseCount; i++) {
            sjpz.push({});
          }
        }
        else if (sjpz.length > caseCount) {
          while (sjpz.length != caseCount) {
            sjpz.pop();
          }
        }

        var homeslist = {
          cpzs: { contents: cpzs, url: cpzs.length == 0 ? '' : cpzs[0].category ? cpzs[0].category.path : "" },
          sjpz: { contents: sjpz, url: sjpz.length == 0 ? '' : sjpz[0].category ? sjpz[0].category.path : "" },
          news: { contents: news, url: news.length == 0 ? '' : news[0].category ? news[0].category.path : "" }
        }

        var series = seriesList.map(function (item) {
          return { 'series': item.value, 'active': item.value == query.series }
        })

        res.render('home', {
          layout: 'layout-default',
          siteInfo: results.siteInfo,
          navigation: results.navigation,
          homeslist: homeslist,
          series: series
        });
      });
    })
  });
};